package com.wenjie.login;

import com.wenjie.common.reRequest.CaptureResponseWrapper;
import com.wenjie.common.utils.TokenUtil;
import com.wenjie.login.annotations.WJLongAspect;
import com.wenjie.original.entity.SysLogOper;
import com.wenjie.original.service.ISysLogOperService;
import com.wenjie.utils.ContextUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.Date;

@Slf4j
@Component
public class LoginInterceptor implements HandlerInterceptor {


    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

        try {

            ISysLogOperService sysLogOperService = ContextUtils.getContext().getBean(ISysLogOperService.class);

            Object attribute = request.getAttribute(WJLongAspect.LOG_FLG); // 获取一下是否要进行拦截

            if (attribute != null) {

                // 获取一下请求的信息
                StringBuilder stringBuilder = new StringBuilder();
                String line;
                BufferedReader reader = request.getReader();
                while ((line = reader.readLine()) != null) {
                    stringBuilder.append(line).append('\n');
                }
                String reqBody = stringBuilder.toString();

                // 获取一下响应的信息
                CaptureResponseWrapper responseWrapper = (CaptureResponseWrapper) response;
                String resBody = responseWrapper.getCapturedResponse();

                // 获取一下是否有状态信息--如果有填入 -如果没有设置为未知
                Object statusObj = request.getAttribute(WJLongAspect.LOG_STATUS);
                SysLogOper.STATUS status;
                if (statusObj == null) status = SysLogOper.STATUS.arg2;
                else status = (SysLogOper.STATUS) statusObj;

                // 获取一下请求进入的开始时间
                Long startTime = Long.valueOf(String.valueOf(request.getAttribute(WJLongAspect.LOG_START_TIME)));
                Long costTime = new Date().getTime() - startTime;

                // 获取一下调用的方法
                String method = String.valueOf(request.getAttribute(WJLongAspect.LOG_METHOD));

                SysLogOper sysLogOper = new SysLogOper();
                sysLogOper.setTitle(String.valueOf(attribute));
                sysLogOper.setSysAdminId(TokenUtil.getTokenInfo().getId());
                sysLogOper.setOperUrl(request.getRequestURL().toString());
                sysLogOper.setMethod(method);
                sysLogOper.setOperParam(reqBody);
                sysLogOper.setJsonResult(resBody);
                sysLogOper.setStatus(status);
                sysLogOper.setCostTime(costTime);
                sysLogOperService.save(sysLogOper);
            }
        } catch (IOException e) {
            log.error(e.getMessage());
        }
        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
    }
}
